package com.dataaccess.dataaccess.support.aibox;
import cn.hutool.json.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.dataaccess.dataaccess.web.service.VisualizationVideoAlarmService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Yang yu
 * @version 1.0
 * @date 2020/9/3 15:38
 */
@Component
public class QueryDevicePictureFile {
    @Resource
    VisualizationVideoAlarmService visualizationVideoAlarmService;
    @Resource
    PictureDownload pictureDownload;
    public  void queryDevicePictureFile (JSONObject jsonObject){
        //获取数据
        JSONObject queryDeviceDetail = jsonObject.getJSONObject("queryDeviceDetail");
        //设备详情数据
        JSONObject data = queryDeviceDetail.getJSONObject("Data");
        if (data!=null){
            //报警信息value
            JSONObject value = jsonObject.getJSONObject("value");
            //CaptureId
            String captureId = value.getStr("AlarmPicID");
            //IotId
            String iotId = data.getStr("IotId");
            DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI4FuyYyG4NhCy9gVuuBnV", "FLHWiYBr84G2wnYIS1shfFeOUXKGrE");
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("linkvisual.cn-shanghai.aliyuncs.com");
            request.setVersion("2018-01-20");
            request.setAction("QueryDevicePictureFile");
            request.putQueryParameter("RegionId", "cn-shanghai");
            request.putQueryParameter("CaptureId", captureId);
            request.putQueryParameter("IotId", iotId);
            try {
                CommonResponse response = client.getCommonResponse(request);
                jsonObject.put("AlarmPicUrl",response);
                System.out.println("++++AlarmPicUrl+++++++:"+response.getData());
                pictureDownload.downloadPicture(jsonObject);
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAI4FuyYyG4NhCy9gVuuBnV", "FLHWiYBr84G2wnYIS1shfFeOUXKGrE");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("linkvisual.cn-shanghai.aliyuncs.com");
        request.setVersion("2018-01-20");
        request.setAction("QueryDevicePictureFile");
        request.putQueryParameter("RegionId", "cn-shanghai");
        request.putQueryParameter("CaptureId", "Rnd1TjRfamdhazB4MFVfUTZPS254SmN1anFETnRBYUdlTFh0RFpRRGt0Zy9hNTBjZWRmYTZiNGI0YThjOGU4Zjg4N2E0MWFmYjUwZV8xNTk5MDM5MTk3MTA2");
        request.putQueryParameter("IotId", "8aKKVRQK7iBgpkmlt7fF000000");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
